package com.example.mobile2030606155byl0408classwork;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


    private TextView tvTitle;
    private CheckBox checkYinqing;
    private CheckBox checkBoli;
    private CheckBox checkFangxiang;
    private Button btnMianBuy;
    private TextView tvTip;
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        checkYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //根据控件的状态，在文本框显示对应信息
                if (isChecked){
                    tvResult.setText("选中："+buttonView.getText().toString());
                }else {
                    tvResult.setText("取消选中："+buttonView.getText().toString());
                }
            }
        });
        checkBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //根据控件的状态，在文本框显示对应信息
                if (isChecked){
                    tvResult.setText("选中："+buttonView.getText().toString());
                }else {
                    tvResult.setText("取消选中："+buttonView.getText().toString());
                }
            }
        });
        checkFangxiang.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //根据控件的状态，在文本框显示对应信息
                if (isChecked){
                    tvResult.setText("选中："+buttonView.getText().toString());
                }else {
                    tvResult.setText("取消选中："+buttonView.getText().toString());
                }
            }
        });
        btnMianBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = "选中";
                //依次判断复选框的选中状态
                if (checkYinqing.isChecked()){
                    msg+="\n"+checkYinqing.getText().toString();
                }
                if (checkBoli.isChecked()){
                    msg+="\n"+checkBoli.getText().toString();
                }
                if (checkFangxiang.isChecked()){
                    msg+="\n"+checkFangxiang.getText().toString();
                }
                tvResult.setText(msg);
            }
        });
    }

    private void initView() {
        tvTitle = findViewById(R.id.tv_title);
        checkYinqing = findViewById(R.id.check_yinqing);
        checkBoli = findViewById(R.id.check_Boli);
        checkFangxiang = findViewById(R.id.check_Fangxiang);
        btnMianBuy = findViewById(R.id.btn_mian_buy);
        tvTip = findViewById(R.id.tv_tip);
        tvResult = findViewById(R.id.tv_result);
    }
}

